﻿#include "StrategyDelegate.h"
#include <QComboBox>
#include <iostream>

StrategyDelegate::StrategyDelegate(const QStringList &pStrategyList, QObject *parent)
	: QItemDelegate(parent)
{
	m_strategyNameList.clear();
	m_strategyNameList.append(pStrategyList);
}

StrategyDelegate::~StrategyDelegate()
{
}
QWidget* StrategyDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
	if (index.column() == 1)           //第1列 班级
	{
		QComboBox   *Cbox = new QComboBox(parent);
		Cbox->addItems(m_strategyNameList);
		return Cbox;
	}

	return QItemDelegate::createEditor(parent, option, index);      //第0列,则选择默认编辑器
}

void StrategyDelegate::setEditorData(QWidget * editor, const QModelIndex & index) const
{
	if (index.column() == 1)           //第1列 班级
	{
		QComboBox   *Cbox = dynamic_cast<QComboBox*>(editor);
		Cbox->setCurrentIndex(Cbox->findText(index.data(Qt::DisplayRole).toString()));
	}
	else
		QItemDelegate::setEditorData(editor, index);
}

void StrategyDelegate::setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const
{
	if (index.column() == 1)           //第1列 班级
	{
		QComboBox   *Cbox = dynamic_cast<QComboBox*>(editor);
		model->setData(index, Cbox->currentText(), Qt::DisplayRole);
	}
	else
		QItemDelegate::setModelData(editor, model, index);
}

void StrategyDelegate::updateEditorGeometry(QWidget * editor, const QStyleOptionViewItem & option, const QModelIndex & index) const
{
	editor->setGeometry(option.rect);
}